﻿using Cartographer.Extensions;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Cartographer.Tiled
{
	public class TiledLayer
	{
		public string Name { get; set; }
		public int X { get; set; }
		public int Y { get; set; }
		public int Width { get; set; }
		public int Height { get; set; }
		public float Opacity { get; set; }
		public bool Visible { get; set; }

		public List<TiledProperty> Properties { get; set; }
		public TiledData Data { get; set; }
		public List<TiledTile> Tiles { get; set; }

		public TiledLayer()
		{
			Properties = new List<TiledProperty>();
		}

		public TiledLayer(XElement element)
		{
			Properties = new List<TiledProperty>();

			Name = element.GetAttribute<string>("name");
			X = element.GetAttribute<int>("x");
			Y = element.GetAttribute<int>("y");
			Width = element.GetAttribute<int>("width");
			Height = element.GetAttribute<int>("height");
			Opacity = element.GetAttribute<float>("opacity");
			Visible = element.GetAttribute<bool>("visible", true);

			Data = new TiledData(element.Element("data"));

			Tiles = new List<TiledTile>();
			foreach (var index in Data.TileIndicies)
			{
				Tiles.Add(new TiledTile((int)index));
			}
		}
	}
}